﻿package component.formula.function;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import component.formula.FormulaInfo;
import component.formula.constant.FormulaConstant;
import component.formula.exception.FormulaException;
import component.formula.util.StringUtil;

public class Select implements IReportFunction {

	public Object calculate(String expression, FormulaInfo formulaInfo, List dataList)
			throws FormulaException {
		List retList = new ArrayList();
		
		String[] paraArr = expression.split(",");
		String colName = paraArr[0].trim(); // 显示的字段名
		String compareName = paraArr[1].trim();  // 用于比较的字段名
		String orderName = ""; // 排序的字段名
		if(paraArr.length>2){
			orderName = paraArr[2]; 
		}
		
		if(!StringUtil.isNull(orderName)){ // 先排序, 对dataList进行排序, 后面的计算也将使用排序后的数据
			
		}
		retList = dataList;
		for(int i = 0;i<retList.size();i++){
			Map rowMap = (Map)retList.get(i);
			Object value = rowMap.get(colName);
			rowMap.put(String.valueOf(formulaInfo.getColNum()), value);
			rowMap.put(FormulaConstant.COMPARE_NAME, String.valueOf(rowMap.get(compareName)));
		}
		
		return retList;
	}


}
